﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsAPP1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 创建一个 OpenFileDialog 实例  
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                // 设置文件类型过滤  
                openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                openFileDialog.Title = "选择要打开的文件";

                // 展示对话框并获取结果  
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        // 读取选择的文件1内容  
                        string filePath = openFileDialog.FileName;
                        string fileContent = File.ReadAllText(filePath);

                        // 显示内容到文本框  
                        textBox1.Text = fileContent;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("无法读取文件内容: " + ex.Message);
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // 创建一个 OpenFileDialog 实例  
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                // 设置文件类型过滤  
                openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                openFileDialog.Title = "选择要打开的文件";

                // 展示对话框并获取结果  
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        // 读取选择的文件2内容  
                        string filePath = openFileDialog.FileName;
                        string fileContent = File.ReadAllText(filePath);

                        // 显示内容到文本框  
                        textBox2.Text = fileContent;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("无法读取文件内容: " + ex.Message);
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox3.Text = textBox1.Text + Environment.NewLine + textBox2.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            // 创建一个 SaveFileDialog 实例  
            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                saveFileDialog.DefaultExt = "txt";
                // 设置文件类型过滤  
                saveFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                saveFileDialog.Title = "另存为";

                // 展示对话框并获取结果  
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        // 获取文本框中的内容  
                        string content = textBox3.Text;

                        // 将其保存到用户选择的文件路径  
                        File.WriteAllText(saveFileDialog.FileName, content);

                        MessageBox.Show("文件已成功保存！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("保存文件时发生错误: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
    }
}
